package com.lyfei.ydc.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyfei.ydc.domain.entity.*;
import com.lyfei.ydc.domain.vo.SpuVo;
import com.lyfei.ydc.exception.http.NotFoundException;
import com.lyfei.ydc.mapper.SpuMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("spuService")
public class SpuService extends BaseService<SpuMapper, Spu> {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SpuImgService spuImgService;

    @Autowired
    private SpecKeyService specKeyService;

    @Autowired
    private SpuAttributeService spuAttributeService;

    @Autowired
    private SkuService skuService;

    public IPage<Spu> getByCategoryId(Long categoryId, Integer current, Integer size) {
        Page<Spu> spuPage = new Page<>(current, size);
        Map<String, Object> params = new HashMap<>();
        params.put("categoryId", categoryId);
        params.put("current", current);
        params.put("size", size);
        return this.baseMapper.selectSpuByPage(spuPage, params);
    }

    public SpuVo detail(Long id) {
        Spu spu = this.getById(id);
        if(spu == null) {
            throw new NotFoundException(30002);
        }

        SpuVo spuVo = new SpuVo();
        BeanUtils.copyProperties(spu, spuVo);

        if(spu.getCategoryId() != null) {
            Category category = categoryService.getById(spu.getCategoryId());
            spuVo.setCategoryName(category.getName());
        }

        if(spu.getSketchSpecId() != null) {
            SpecKey specKey = specKeyService.getById(spu.getSketchSpecId());
            spuVo.setSketchSpecName(specKey.getName());
        }

        // 查询sku信息
        QueryWrapper<Sku> skuQueryWrapper = new QueryWrapper<>();
        skuQueryWrapper.eq("spu_id", spu.getId());
        List<Sku> skuList = skuService.list(skuQueryWrapper);
        spuVo.setSkuList(skuList);
        skuList.forEach(item -> {
            if(item.getImg() == null) {
                item.setImg(spu.getImg());
            }
        });

        QueryWrapper<SpuImg> spuImgQueryWrapper = new QueryWrapper<>();
        spuImgQueryWrapper.eq("spu_id", spu.getId());
        List<SpuImg> spuImgList = spuImgService.list(spuImgQueryWrapper);
        spuVo.setSpuImgList(spuImgList);

        QueryWrapper<SpuAttribute> spuAttributeQueryWrapper = new QueryWrapper<>();
        spuAttributeQueryWrapper.eq("spu_id", spu.getId());
        spuAttributeQueryWrapper.orderByAsc("id");
        List<SpuAttribute> spuAttributeList = spuAttributeService.list(spuAttributeQueryWrapper);
        spuVo.setSpuAttributes(spuAttributeList);

        return spuVo;
    }
}
